package com.gdit.zax.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.dao.SchoolDao;
import com.gdit.zax.entitys.BaseEntity;
import com.gdit.zax.entitys.StartPic;
import com.gdit.zax.utils.DBUtil;

public class SchoolDaoImpl implements SchoolDao {
	private PreparedStatement ps;
	private ResultSet rs;
	
	private Connection conn;
	private HttpServletRequest request;
	private HttpServletResponse response;
	
	
	public SchoolDaoImpl(Connection conn,HttpServletRequest request, HttpServletResponse response) {
		this.conn = conn;
		this.request = request;
		this.response = response;
	}
	
	public boolean addSchool(String sname) {
		String sql = "insert t_school set Name=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1,sname);
			
			int result = ps.executeUpdate();
			if (result!=0) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public ArrayList<BaseEntity> getAllSchool() {
		ArrayList<BaseEntity> list = new ArrayList<BaseEntity>();
		String sql = "select * from t_school";
		try {
			ps = conn.prepareStatement(sql);
			
			rs = ps.executeQuery();
			while(rs.next()){
				list.add(DBUtil.getobject(rs, BaseEntity.class));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public boolean isSchoolExist(String sname) {
		String sql = "select * from t_school where Name=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, sname);
			
			rs = ps.executeQuery();
			if(rs.next()){
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public BaseEntity getSchoolInfo(String name) {
		String sql = "select * from t_school where Name=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, name);
			
			rs = ps.executeQuery();
			if(rs.next()){
				return DBUtil.getobject(rs, BaseEntity.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
